﻿/******************************************************************************
* File description: DeviceCheckInfo.qml
* Function: 小黄条
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.12
import QtQuick.Templates 2.12
import QtGraphicalEffects 1.14
import "../components"

Rectangle{

    id: root

    property var infoTitleList: [
        ["靶材罐", "气泵压力", "挡板"],
        ["靶材罐", "气泵压力", "进靶气缸", "梭体上靶气缸", "水冷机"],
        ["靶材罐", "挡板", "吸盘状态", "夹爪", "靶材盖", "液位", "溶靶瓶", "靶材片吸取"],
        ["左压紧", "右压紧", "耗材触点", "废液瓶", "产物瓶", "能谱检测"]
    ]
    property var infoValueList: [
        ["有", "夹紧", "关"],
        ["有", "否", "否"],
        ["有", "夹紧", "关"],
        ["是", "0Mpa", "无", "无", "无", "无", "无"]
    ]

    // 压力值，我不想再取名字了
    property string yaLiZhi13: "--"
    property string yaLiZhi1: "--"

    property var fuSheShuJu: ["--", "--", "--", "--", "--"]

    Connections{
        target: CSignal
        onSendRadiationData: {
//            console.log("接收到辐射数据", module, radiationData)
            fuSheShuJu[module] = radiationData
            if(module === 1){
                fuSheShuJu[3] = radiationData
            }
            fuSheShuJuChanged()
        }
    }

    property var infoTitle: {

        if(gPageIdx>=1 && gPageIdx<=4){
            return infoTitleList[gPageIdx-1]
        }else{
            return []
        }
    }
    property var infoValue: {

        if(gPageIdx>=1 && gPageIdx<=4){
            return infoValueList[gPageIdx-1]
        }else{
            return []
        }
    }

    Timer{
        id: timer_refreshCheckInfo
        repeat: true
        running: true
        interval: 500
        onTriggered: {
            refreshCheckInfo()
        }
    }

    Connections{
        target: mainwindow
        onGPageIdxChanged: {
            refreshCheckInfo()
        }
    }
    Connections{            // 放靶气泵和打靶气泵的压力值（我也不知道这俩信号为啥在这儿）
        target: CSignal
        onTargetGasPumpValue: {     // 放靶气泵压力值
            yaLiZhi13 = value
            yaLiZhi1 = value
        }
//        onTargetAirPumpValue: {    // 打靶气泵压力值
//            yaLiZhi1 = value
//        }
    }


    /** @brief 从后端重新获取设备信息 */
    function refreshCheckInfo(){

        var infoList = CAnimationPractice.getCheckInfo(gPageIdx)
        infoValue = ["--", "--", "--", "--", "--", "--", "--", "--", "--", "--", "--"]
        if(gConnectStatus === false){
            return
        }
        for(let key in infoList){
            if(key === ""){     // 处理后端的列表不知道为什么有一个空的问题
                continue
            }
            if(gPageIdx === 3){
                infoValue[Number(key)+2] = infoList[key]      // 转靶页面去放靶页面获取靶材罐的状态，其它部分顺位+1
            }else{
                infoValue[Number(key)] = infoList[key]
            }
        }


        if(gPageIdx === 3){
            var list1 = CAnimationPractice.getCheckInfo(1)
            infoValue[0] = list1["0"]
            infoValue[1] = list1["2"]
        }
        infoValueChanged()
    }

//    color: "#FDF6EC"
    color: "#EEEEEE"
    height: 40*dp
    width: parent.width
    CustomImage{
        source: "qrc:/picture/bell.png"
        height: 18*dp; width: 18*dp
        anchors.verticalCenter: parent.verticalCenter
        x: 20*dp
        visible: gPageIdx !== 0
    }
    // 左边的信息
    Row{
        id: layout_expProInfo
        x: 60*dp
        anchors.verticalCenter: parent.verticalCenter
        Repeater{
            model: Math.min(infoTitle.length, infoValue.length)
            Row{
                Text {
                    text: infoTitle[index] + "("
                    font.pixelSize: config.fontsize_a
                }
                Text{
                    text: infoValue[index]
                    color: {
                        if (text==="无" || text==="否" || text==="--" || text==="异常" || text==="故障")       return "red"
                        else if(text==="屏蔽")               return "#666"
                        else                                return "green"
                    }
                    font.pixelSize: config.fontsize_a
                }Text{
                    text: ") | "
                    font.pixelSize: config.fontsize_a
                }
            }
        }
        // 单独再加一个 放靶/打靶/转靶气泵压力值 的实时显示
        Row{
            visible: gPageIdx !== 4 && gPageIdx !== 0
            Text {
                text: {
                    if(gPageIdx === 1 || gPageIdx === 3)      return "气泵压力值" + "("
                    else                                      return "气泵压力值" + "("
                }
                font.pixelSize: config.fontsize_a
            }
            Text{
                text: {
                    if(gPageIdx === 1 || gPageIdx === 3)      return yaLiZhi13
                    else                                      return yaLiZhi1
                }
                color: {
                    if (text==="无" || text==="否" || text==="--" || text==="异常" || text==="故障")       return "red"
                    else if(text==="屏蔽")               return "#666"
                    else                                return "green"
                }
                font.pixelSize: config.fontsize_a
            }Text{
                text: ") | "
                font.pixelSize: config.fontsize_a
            }
        }


        // 单独再加一个 辐射数据的显示
        Row{
            visible: gPageIdx !== 0
            Text {
                text: "辐射" + "("
                font.pixelSize: config.fontsize_a
            }
            Text{
                text: fuSheShuJu[gPageIdx]
                color: {
                    if (text==="无" || text==="否" || text==="--" || text==="异常" || text==="故障")       return "red"
                    else if(text==="屏蔽")               return "#666"
                    else                                return "green"
                }
                font.pixelSize: config.fontsize_a
            }
            Text{
                text: "μSv"
                font.pixelSize: config.fontsize_a
                color: fuSheShuJu[gPageIdx]==="--"?"red":"green"
            }

            Text{
                text: ") | "
                font.pixelSize: config.fontsize_a
            }
        }
        // 单独再加一个，活检的浓度
        Row{
            visible: gPageIdx === 3
            Text {
                text: "活检浓度" + "("
                font.pixelSize: config.fontsize_a
            }
            Text{
                text: gStateExp.hoJianConc
                color: {
                    if (text==="无" || text==="否" || text==="--" || text==="异常" || text==="故障")       return "red"
                    else if(text==="屏蔽")               return "#666"
                    else                                return "green"
                }
                font.pixelSize: config.fontsize_a
            }
            Text{
                text: ")"
                font.pixelSize: config.fontsize_a
            }
        }


//        Connections{
//            target: CSignal
//            onMaintenanceInfoChanged: {
//                if(type==="压紧"){
//                    layout_expProInfo.infoValue[0] = value
//                }if(type==="压力值"){
//                    layout_expProInfo.infoValue[1] = value
//                }if(type==="芯片耗材"){
//                    layout_expProInfo.infoValue[2] = value
//                }if(type==="能谱检测"){
//                    layout_expProInfo.infoValue[3] = value
//                }if(type==="废液瓶"){
//                    layout_expProInfo.infoValue[4] = value
//                }if(type==="产物瓶"){
//                    layout_expProInfo.infoValue[5] = value
//                }
//                layout_expProInfo.infoValueChanged()
//            }
//        }
    }
    // 靠右的信息
    Row{
        x: parent.width - width - 30*dp
        height: parent.height
        spacing: 12*dp
        anchors.verticalCenter: parent.verticalCenter

        // 故障信息
        Rectangle{
            id: info_failureInfo
            color: "transparent"
            radius: 5*dp
            width: 90*dp; height: parent.height - 10*dp
            anchors.verticalCenter: parent.verticalCenter

            Text{
                id: text_failureInfo
                text: "故障信息(" + box_failureInfo2.failureInfoList.length + ")"
                font.pixelSize: config.fontsize_a
                anchors.centerIn: parent
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: config.hoverEnabled
                onEntered: { parent.color = "#11111111" }
                onExited: { parent.color = "transparent" }
                cursorShape: "PointingHandCursor"
                onClicked: {
                    box_failureInfo2.show()
                }
            }
        }

        // 通信状态
        Rectangle{
            id: info_connect
            radius: 5*dp
            color: "transparent"
            width: row_connect.implicitWidth + 18*dp; height: parent.height - 10*dp
            anchors.verticalCenter: parent.verticalCenter
            Row{
                id: row_connect
                spacing: 5*dp
                anchors.centerIn: parent
                Text{
                    text: '<font color="000">通信状态: </font>'
                    font.pixelSize: config.fontsize_a
                    anchors.verticalCenter: parent.verticalCenter
                }CustomImage{
                    height: 18*dp; width: 18*dp
                    source: gConnectStatus ? "qrc:/picture/connect_g.png" : "qrc:/picture/connect_r.png"
                    anchors.verticalCenter: parent.verticalCenter
                }Text{
                    text: gConnectStatus ? '<font color="green"">正常</font>' : '<font color="red"">异常</font>'
                    font.pixelSize: config.fontsize_a
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: config.hoverEnabled
                onEntered: { parent.color = "#11111111" }
                onExited: { parent.color = "transparent" }
                cursorShape: "PointingHandCursor"
                onClicked: {
                    if(gExpIsRunning === 0){
                        preset.close()
                        preset.show()
                        preset.curPage = 3
                    }else{
                        toast.show("当前无法修改设备连接状态", 2)
                    }
                }
            }
        }
        // 用户信息
        Rectangle{
            radius: 5*dp
            color: "transparent"
            height: parent.height - 10*dp; width: row_usr.implicitWidth + 18*dp
            anchors.verticalCenter: parent.verticalCenter
            Row{
                id: row_usr
                spacing: 2*dp
                anchors.centerIn: parent
                anchors.verticalCenter: parent.verticalCenter
                Text{
                    id: text_usr
                    text: "用户名: " + CLicense.getSystemLicenseInfo()["UserName"]
                    font.pixelSize: config.fontsize_a
                    anchors.verticalCenter: parent.verticalCenter
                }Image{
                    height: 22*dp; width: 22*dp
                    anchors.verticalCenter: parent.verticalCenter
                    source: "qrc:/picture/uparrow.png"
                }
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: config.hoverEnabled
                onEntered: { parent.color = "#11111111" }
                onExited: { parent.color = "transparent" }
                cursorShape: "PointingHandCursor"
                onClicked: {
                    console.log("用户信息" + CLicense.getSystemLicenseInfo()["UserName"])
                    popup_usrInfo.open()
                }
            }
            Popup{
                id: popup_usrInfo
                x: parent.width-width; y: -height - 10*dp
                height: 110*dp; width: 200*dp

                background: Rectangle{
                    radius: 5*dp
                    layer.enabled: true
                    onVisibleChanged: {
                    }
                    Column{
                        anchors.centerIn: parent
                        spacing: 10*dp
                        Text {
                            text: "用户名: " + CLicense.getSystemLicenseInfo()["UserName"]
                            font.pixelSize: config.fontsize_a
                        }Text{
                            text: "有效日期: " + CLicense.getSystemLicenseInfo()["LimitDate"]
                            font.pixelSize: config.fontsize_a
                        }
                    }
                    layer.effect: DropShadow {
                        radius: 21*dp
                        samples: 43
                        transparentBorder: true
                        color: "#2D666666"
                    }
                }
            }
        }
    }
    Rectangle{
        height: 1
        width: parent.width
        color: "#11111111"
    }
}
